package com.example.apple.simpleadapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ListView mainlv;

    /**
     * HashMap集合来接收数据
     * */
    private List<Map<String,Object>> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainlv= (ListView) findViewById(R.id.main_lv);
        data=new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            HashMap<String,Object> info=new HashMap<>();
            /**
             * 图片+图片id名字
             * */
            info.put("img",R.mipmap.ic_launcher);
            /**
             * 放置数据
             * */
            info.put("title","这是标题"+i);
            /**
             * 把info 放到data里
             * */
        data.add(info);
        }

        /**
         * 创建SimpleAdapter
         * 参数：1上下文 2 数据源 3 子布局文件 4key值所在的数组 5子布局里控件的id
         * */
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,data,R.layout.item_main,new String[]{"img","title"},new int[]{R.id.item_iv,R.id.item_tv});
    /**
     * 绑定SimpleAdapter  设置到ListView上
     * */
    mainlv.setAdapter(simpleAdapter);

    }
}
